﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data;
using System.Linq;
using System.Text;

using MSDIS.Configuration.Entities;

namespace MSDIS.Configuration.DAO
{
    internal class VariableDao
    {
        private IObjectSet<VariableEntity> entities;

        internal VariableEntity GetVariableById(int id)
        {
            using (var context = new ConfigurationEntities())
            {
                context.Connection.Open();
                entities = context.CreateObjectSet<VariableEntity>();
                return entities.Single(e => e.Id == id);
            }
        }

        internal IEnumerable<VariableEntity> GetVariables()
        {
            using (var context = new ConfigurationEntities())
            {
                context.Connection.Open();
                entities = context.CreateObjectSet<VariableEntity>();
                return entities.ToList();
            }

            
        }

        internal void Add(VariableEntity entity)
        {
            using (var context = new ConfigurationEntities())
            {
                context.Connection.Open();
                entities = context.CreateObjectSet<VariableEntity>();
                entities.AddObject(entity);
                Save(context, entity);
            }
        }

        internal void Update(VariableEntity newEntity)
        {
            using (var context = new ConfigurationEntities())
            {
                context.Connection.Open();
                entities = context.CreateObjectSet<VariableEntity>();
                VariableEntity entity = entities.Where(e => e.Id == newEntity.Id).First();
                entity.Name = newEntity.Name;
                entity.Value = newEntity.Value;
                entity.DefaultValue = newEntity.DefaultValue;
                entity.LastUpdated = newEntity.LastUpdated;
                Save(context, entity);
            }
        }

        internal void Delete(int id)
        {
            using (var context = new ConfigurationEntities())
            {
                context.Connection.Open();
                entities = context.CreateObjectSet<VariableEntity>();
                VariableEntity entity = entities.Single(e => e.Id == id);
                entities.DeleteObject(entity);

                Save(context, entity);
            }
        }

        internal void Save(ObjectContext context, object entity)
        {
            using (var transaction = context.Connection.BeginTransaction())
            {
                try
                {
                    context.SaveChanges();
                    transaction.Commit();
                }
                catch (OptimisticConcurrencyException)
                {
                    if (context.ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Deleted || context.ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Modified)
                        context.Refresh(RefreshMode.StoreWins, entity);
                    else if (context.ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Added)
                        context.Detach(entity);

                    context.AcceptAllChanges();
                    transaction.Commit();
                }

                context.Connection.Close();
            }
        }
    }
}
